package com.example.owner.lettery1.ui;

import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.example.owner.lettery1.R;
import com.example.owner.lettery1.base.BaseActivity;

import butterknife.OnClick;

public class BannerActivity extends BaseActivity {

    WebView mWebview;
    WebSettings mWebSettings;

    @Override
    public int getLayoutId() {
        return R.layout.activity_banner;
    }

    @Override
    protected void init() {

        mWebview = findViewById(R.id.web_view);
        mWebSettings = mWebview.getSettings();

        mWebview.loadUrl("http://m.cpb11.com/index.php/phone/youhuiItem/id/8/type/4");

        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }


    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) {
            mWebview.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebview != null) {
            mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebview.clearHistory();

            ((ViewGroup) mWebview.getParent()).removeView(mWebview);
            mWebview.destroy();
            mWebview = null;
        }
        super.onDestroy();
    }

    @OnClick(R.id.bt_back)
    public void onViewClicked() {
      this.finish();
    }
}
